package com.ys168.gam.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.ys168.gam.bean.EngineInfo;
import com.ys168.gam.bean.TypeInfo;

/**
 * 
 * @author Jason Krebs
 * @since 2015年6月8日
 */
public class ConfigLoader {

    private static final int DEFAULT_PAGE_SIZE = 10;

    public static final int DEFAULT_RESULT_COUNT = 20;

    private static Map<String, EngineInfo> engines;

    private static int searchTimeout;

    private static int downloadTimeout;

    private static String helpInfo;

    private static String ENGINE = "engine";

    private static List<TypeInfo> createListData(String data, boolean isCustomImageSize) {
        List<TypeInfo> list = new ArrayList<TypeInfo>();

        for (String item : data.split("\\|")) {
            String[] d = item.split("-");
            if (d.length != 2) {
                throw new Error("Data Format Error:" + data);
            }
            list.add(new TypeInfo(d[0], d[1]));
        }
        if (isCustomImageSize) {
            list.add(new TypeInfo("", "自定义尺寸"));
        }

        return list;
    }

    /**
     * 单位：秒
     * 
     * @return
     */
    public static int getDownloadTimeout() {
        return downloadTimeout;
    }

    public static Collection<String> getEngineCodes() {
        return new ArrayList<String>(engines.keySet());
    }

    public static EngineInfo getEngineInfo(String code) {
        return engines.get(code);
    }

    public static String getHelpInfo() {
        return helpInfo;
    }

    /**
     * 单位：秒
     * 
     * @return
     */
    public static int getSearchTimeout() {
        return searchTimeout;
    }

    private static void initEngineInfo(Properties properties) {
        engines = new HashMap<String, EngineInfo>();

        String engineStr = properties.getProperty(ENGINE);
        for (String code : engineStr.split(",")) {
            String name = ENGINE + "." + code + "." + "name";
            String url = ENGINE + "." + code + "." + "url";
            String searcherClazz = ENGINE + "." + code + "." + "searcher";
            String pageSize = ENGINE + "." + code + "." + "pageSize";
            String imageSize = ENGINE + "." + code + "." + "imgSize";
            String allowCustomSize = ENGINE + "." + code + "." + "allowCustomSize";

            EngineInfo engineInfo = new EngineInfo(code, properties.getProperty(name), properties.getProperty(url));
            engineInfo.setSearcherClazz(properties.getProperty(searcherClazz));

            String pageSizeStr = properties.getProperty(pageSize);
            engineInfo.setPageSize((pageSizeStr == null) ? DEFAULT_PAGE_SIZE : Integer.parseInt(pageSizeStr));

            boolean isCustomImageSize = "1".equals(properties.getProperty(allowCustomSize))
                    || "true".equals(properties.getProperty(allowCustomSize));
            engineInfo.setCustomImageSize(isCustomImageSize);
            engineInfo.setImageSizeData(createListData(properties.getProperty(imageSize), isCustomImageSize));

            engines.put(code, engineInfo);
        }
    }

    private static void initOtherInfo(Properties properties) {
        searchTimeout = 1000 * Integer.parseInt((String) properties.get("search.timeout"));
        downloadTimeout = 1000 * Integer.parseInt((String) properties.get("download.timeout"));
        helpInfo = (String) properties.get("help.info");
    }

    public static void load() {
        Properties properties = new Properties();
        try {
            InputStream input = ConfigLoader.class.getResourceAsStream("/res/config.properties");
            properties.load(new InputStreamReader(input, "UTF-8"));
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        initEngineInfo(properties);
        initOtherInfo(properties);
    }
}
